package com.test.nnooolwdeaer.activity_fragment_wooden.base_wooden;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.webkit.WebView;

import com.test.nnooolwdeaer.constant_wooden.Constant_wooden;
import com.test.nnooolwdeaer.util_wooden.event.TrackManager_wooden;
import com.test.nnooolwdeaer.util_wooden.helper_wooden.DeviceHelper_wooden;
import com.test.nnooolwdeaer.util_wooden.helper_wooden.UserInfoHelper_wooden;

import org.jetbrains.annotations.NotNull;

public class BaseApplication_wooden extends Application {

    @NotNull
    public static BaseApplication_wooden getApplication() {
        return baseApplication_wooden;
    }

    /**
     * 适配android 9 网络进程
     */
    private void initPieWebView_wooden() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            String processName = getProcessName_wooden(this);
            if (!DeviceHelper_wooden.getPackageName_wooden().equals(processName)) {
                WebView.setDataDirectorySuffix(processName);
            }
        }
    }

    private String getProcessName_wooden(Context context) {
        if (context == null) return null;
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (manager != null) {
            for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
                if (processInfo.pid == android.os.Process.myPid()) {
                    return processInfo.processName;
                }
            }
        }
        return "";
    }

    private static BaseApplication_wooden baseApplication_wooden;

    @Override
    public void onCreate() {
        super.onCreate();
        baseApplication_wooden = this;
        try {

            //tag2025
//            FirebaseApp.initializeApp(this);

            TrackManager_wooden.INSTANCE.init();

            //pie webview 适配
            initPieWebView_wooden();

            //user info
            UserInfoHelper_wooden.getInstance_wooden().init_wooden(getApplicationContext());

            //syc data sdk
            initSycDataSDK_wooden();


        } catch (Exception e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
    }

    public void initSycDataSDK_wooden() {

    }


    public int versionCode(){

        try {
            return getPackageManager().getPackageInfo(getPackageName(),0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
        return 0;
    }

    public String versionCodeStr(){

        try {
            return String.valueOf(getPackageManager().getPackageInfo(getPackageName(),0).versionCode);
        } catch (PackageManager.NameNotFoundException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
        return "0";
    }

    public String versionName(){
        try {
            return getPackageManager().getPackageInfo(getPackageName(),0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            if (Constant_wooden.Debug) e.printStackTrace();
        }
        return "";
    }

}
